<template>
  <div class="chart-container2">
    <div ref="chart" class="chart2"></div>
  </div>
</template>
  
<script>
import * as echarts from 'echarts';

export default {
  props: {
    data: {
      type: Array,
      required: true
    }
  },
  mounted() {
    this.initChart();
  },
  watch: {
    data: {
      handler() {
        this.initChart();
      },
      deep: true,
    },
  },
  methods: {
    initChart() {
      const chartDom = this.$refs.chart;
      const myChart = echarts.init(chartDom, "");
      const categories = this.data.map((item) => item.day);
      const uniqueDays = [...new Set(categories)];
      const requests11 = this.data.filter((item) => item.hour === 11).map((item) => item.requests);
      const requests12 = this.data.filter((item) => item.hour === 12).map((item) => item.requests);
      const requests13 = this.data.filter((item) => item.hour === 13).map((item) => item.requests);

      myChart.setOption({
        title: {
          text: '不同时段请求数'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['11', '12', '13']
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        toolbox: {
          feature: {
            saveAsImage: {}
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: true,
          data: uniqueDays 
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '11',
            type: 'line',
            stack: 'Total',
            data: requests11
          },
          {
            name: '12',
            type: 'line',
            stack: 'Total',
            data: requests12
          },{
            name: '13',
            type: 'line',
            stack: 'Total',
            data: requests13
          },
        ]
      })
    }
  }
};
</script>
  
<style>
/* 可以根据需要添加样式 */
</style>
  